package fun.easycode.webterminal.adapter.api;

import fun.easycode.webterminal.common.contract.R;
import fun.easycode.webterminal.service.SftpService;
import fun.easycode.webterminal.service.dto.SftpDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Tag(name = "SFTP")
@RestController
@RequestMapping("/api/sftp")
public class SftpApi {

    private final SftpService sftpService;

    @Autowired
    public SftpApi(SftpService sftpService) {
        this.sftpService = sftpService;
    }

    @GetMapping("/ls")
    @Operation(summary = "遍历目录")
    public R<SftpDTO> ls(@RequestParam String id, @RequestParam(defaultValue = "/") String path) {
        return sftpService.ls(id, path);
    }

    @SneakyThrows
    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public R<?> upload(@RequestParam String id
            , @RequestParam String path
            , @RequestParam("file") MultipartFile file) {
        return sftpService.upload(id, path, file.getOriginalFilename(), file.getInputStream());
    }


    @SneakyThrows
    @PostMapping("/upload-sse")
    @Operation(summary = "上传文件(sse实时进度)")
    public SseEmitter uploadSSE(@RequestParam String id
            , @RequestParam String path
            , @RequestParam("file") MultipartFile file) {

        SseEmitter emitter = new SseEmitter(0L);

        sftpService.uploadSSE(id, path, file.getOriginalFilename(), file, emitter);

        return emitter;
    }


    @PostMapping("/delete")
    @Operation(summary = "删除文件")
    public R<?> delete(@RequestParam String id, @RequestParam String path) {
        return sftpService.delete(id, path);
    }

    @SneakyThrows
    @GetMapping("/download")
    @Operation(summary = "下载文件")
    public void download(@RequestParam String id, @RequestParam String path, HttpServletResponse response) {


        // 截取path最后/后面的文件名
        String fileName = path.substring(path.lastIndexOf("/") + 1);

        // 对文件名进行URL编码
        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);

        // 设置响应头编码
        response.setHeader(HttpHeaders.CONTENT_TYPE, "application/octet-stream; charset=UTF-8");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + encodedFileName);

        sftpService.download(id, path, response.getOutputStream(), (size) -> {
            response.addHeader("Content-Length", "" + size);
        });
    }

}
